package com.diandi.code.controller;

import com.diandi.code.service.CodeAutoService;
import com.diandi.code.service.DbAutoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 袁恩光 on 2018/5/7.
 */
@RestController
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private CodeAutoService codeAutoService;

    @Autowired
    private DbAutoService dbAutoService;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("data", "系统请求时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return modelAndView;
    }


    @RequestMapping(value = "getTemplate", method = RequestMethod.POST)
    public ResponseEntity<byte[]> getDemo(@RequestParam("modelName") String modelName) {
        logger.info("请求生成基础项目:" + modelName);
        try {
            return codeAutoService.getResponseEntity(modelName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping(value = "getDbTemplate", method = RequestMethod.POST)
    public ResponseEntity<byte[]> getDbDemo(@RequestParam("modelName") String modelName) {
        logger.info("请求生成基础db配置:" + modelName);
        try {
            return dbAutoService.getResponseEntity(modelName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
